﻿using System;
using System.Globalization;

namespace LsTool
{
    /// <summary>
    /// 时间转换
    /// </summary>
    public static class DateTimeCom
    {
        /// <summary>
        /// Linux时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp()
        { 
            return GetTimeStampToLong().ToString();
        }   
        
        /// <summary>
        /// Linux时间戳
        /// </summary>
        /// <returns></returns>
        public static DateTime GetTimeStampToDateTime(long timestamp)
        {
            return DateTime.MinValue.AddHours(8).AddSeconds(62135596800 + timestamp);
        } 
        /// <summary>
        /// Linux时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStampToLong()
        { 
            return GetTimeStampToLong(DateTime.Now);
        } 
        
        /// <summary>
        /// Linux时间戳
        /// </summary>
        /// <returns></returns>
        public static long GetTimeStampToLong(DateTime dt)
        {
            return (dt.ToUniversalTime().Ticks - 621355968000000000) / 10000000;
            
        }
        /// <summary>
        /// Linux时间戳
        /// </summary>
        /// <returns></returns>
        public static string GetTimeStamp(int x)
        { 
            return (GetTimeStampToLong() / x).ToString();
        }

        /// <summary>
        /// 时间转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime YyyyMMddHHmmssToDateTime(string str)
        {
            return DateTime.ParseExact(str, "yyyyMMddHHmmss", CultureInfo.CurrentCulture);
        }
        /// <summary>
        /// 时间转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime YyyyMMddHHmmToDateTime(string str)
        {
            return DateTime.ParseExact(str, "yyyyMMddHHmm", CultureInfo.CurrentCulture);
        }
        /// <summary>
        /// 时间转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime YyyyMMddToDateTime(string str)
        {
            return DateTime.ParseExact(str, "yyyyMMdd", CultureInfo.CurrentCulture);
        } /// <summary>
        /// 时间转换
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static DateTime HHmmssToDateTime(string str)
        {
            return DateTime.ParseExact(str, "HHmmss", CultureInfo.CurrentCulture);
        }
    }
}